package com.rew.canteenbox.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.rew.canteenbox.R;
import com.rew.canteenbox.bases.AppData;
import com.rew.canteenbox.models.Circle;
import com.rew.canteenbox.models.User;

import java.util.List;

import static com.rew.canteenbox.bases.AppData.ACTIVE;
import static com.rew.canteenbox.bases.AppData.GRAY;

/**
 *  Circle Adapter.
 *
 * Created by HuiWen Ren on 2016/11/24.
 */
public class CircleAdapter extends ArrayAdapter<Circle> {

    private int resourceId;
    private AppData appData;

    private View view;

    public CircleAdapter(Context context, int textViewResourceId,
                          List<Circle> objects) {
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;
        appData = (AppData) getContext().getApplicationContext() ;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Circle circle = getItem(position);
        User user = circle.getUser();
        ViewHolder viewHolder = new CircleAdapter.ViewHolder();
        if (convertView == null) {
            viewInit(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag(); // 重新获取ViewHolder.
        }
        // 设置资源.
        viewHolder.userName.setText(user.getName());
        viewHolder.userHeadImg.setImageResource(user.getHeadImg());
        viewHolder.circleBodyText.setText(circle.getText());

        fresh(position, viewHolder);

        viewHolder.like.setTag(position);
        ViewHolder finalViewHolder = viewHolder;
        viewHolder.like.setOnClickListener(v -> {
            changeSt(position, finalViewHolder);
            appData.chLike(position);
            fresh(position, finalViewHolder);
        });

        return view;
    }

    class ViewHolder{
        TextView userName;
        ImageView userHeadImg;
        TextView circleBodyText;
        LinearLayout like;
        LinearLayout comment;
        TextView likeNum;
        TextView likeSta;
        ImageView likeImg;
    }

    private void fresh(int position, ViewHolder viewHolder){
        int k = appData.getCircleList().get(position).likeNum;
        if(k == 0){
            viewHolder.likeNum.setVisibility(View.GONE);
        } else {
            viewHolder.likeNum.setVisibility(View.VISIBLE);
            viewHolder.likeNum.setText("(" + Integer.toString(k) + ")");
        }
    }

    private void changeSt(int position, ViewHolder viewHolder){
        Circle circle = appData.getCircleList().get(position);
        if(!circle.likeMark){
            viewHolder.likeSta.setText("已赞");
            viewHolder.likeSta.setTextColor(ACTIVE);
            viewHolder.likeNum.setTextColor(ACTIVE);
            viewHolder.likeImg.setImageResource(R.drawable.pressed_default);
            appData.addLike(position);
        } else {
            viewHolder.likeSta.setText("赞");
            viewHolder.likeSta.setTextColor(GRAY);
            viewHolder.likeNum.setTextColor(GRAY);
            viewHolder.likeImg.setImageResource(R.drawable.button_default);
            appData.disLike(position);
        }
    }

    private void viewInit(ViewHolder viewHolder){
        view = LayoutInflater.from(getContext()).inflate(resourceId, null);
        // 对应布局.
        viewHolder.userName = (TextView) view.findViewById
                (R.id.tv_circle_name);
        viewHolder.userHeadImg = (ImageView) view.findViewById
                (R.id.iv_circle_img);
        viewHolder.circleBodyText = (TextView) view.findViewById
                (R.id.tv_circle_body);
        viewHolder.like = (LinearLayout) view.findViewById
                (R.id.ll_like);
        viewHolder.comment = (LinearLayout) view.findViewById
                (R.id.ll_comment);
        viewHolder.likeNum = (TextView) view.findViewById
                (R.id.tv_likeNum);
        viewHolder.likeImg = (ImageView) view.findViewById
                (R.id.iv_like);
        viewHolder.likeSta = (TextView) view.findViewById
                (R.id.tv_like);
        view.setTag(viewHolder); // 将ViewHolder存储在View中.
    }
}
